from django.shortcuts import render
from django.views.decorators.http import require_POST, require_GET
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from doctor.models import Hospital, Doctor, History1
from doctor.serial import HospitalSerializer, DoctorSerializer, History1Serializer
from doctor.service import initDoctor, get_message
from rest_framework.permissions import IsAuthenticated
from dingxiang_doctor.utils.myredis import r


# Create your views here.
class HospitalListAPIView(ListAPIView):
    queryset = Hospital.objects.all()
    serializer_class = HospitalSerializer


class HospitalRetrieveAPIView(RetrieveAPIView):
    queryset = Hospital.objects.all()
    serializer_class = HospitalSerializer

# 处理医院数据
class HospitalAPIView(APIView):
    def patch(self, request, pk):
        hospital = Hospital.objects.get(id=pk)
        name = request.data.get("name")
        address = request.data.get("address")
        description = request.data.get("description")
        if not name:
            name = hospital.name
        if not address:
            address = hospital.address
        if not description:
            description = hospital.description
        data = {"name": name, "address": address, "description": description}
        serializer = HospitalSerializer(hospital, data=data, partial=True)
        if serializer.is_valid():
            serializer.save()
            # 将数据存入redis，准备更新es
            es_id = hospital.es_id
            r.hash_add(f"hospital:{es_id}", "id", hospital.id)
            r.hash_add(f"hospital:{es_id}", "type", "hospital")
            r.hash_add(f"hospital:{es_id}", "name", data["name"])
            return Response({"msg": "ok", "data": serializer.data})
        return Response({"msg": "error", "data": serializer.errors})


# 获取某一医院的医生
class DoctorHospitalListAPIView(ListAPIView):
    serializer_class = DoctorSerializer

    def get_queryset(self):
        hospital = self.request.query_params.get("hospital")
        queryset = Doctor.objects.filter(hospital=hospital)
        return queryset


# 获取某一科室的医生
class DoctorDepartmentListAPIView(ListAPIView):
    serializer_class = DoctorSerializer

    def get_queryset(self):
        department = self.request.query_params.get("department")
        queryset = Doctor.objects.filter(department=department)
        return queryset

# 获取某一医生信息
class DoctorRetrieveAPIView(RetrieveAPIView):
    queryset = Doctor.objects.all()
    serializer_class = DoctorSerializer

# 询问医生
class Consultation(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        user_id = request.user.id
        doctor_id = request.query_params.get("doctor_id")
        name = request.query_params.get("name")
        department = request.query_params.get("department")
        data = initDoctor(name, department, user_id, doctor_id)
        return Response({"msg": "ok", "user_id": user_id, "data": data})

    # def post(self, request):
    #     print("111111111")
    #     message = request.data.get("message")
    #     send_back = get_message(message)
    #     return Response({"msg": "ok", "data": send_back})


@require_GET
def query(request):
    message = request.GET.get("message")
    user_id = request.GET.get("user_id")
    doctor_id = request.GET.get("doctor_id")
    return get_message(message, user_id, doctor_id)
